package com.company.project.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.company.project.core.Result;
import com.company.project.dao.VtManagerMapper;
import com.company.project.model.VO.OperatorVO;
import com.company.project.model.VO.UserVO;
import com.company.project.model.VtManager;
import com.company.project.service.VtManagerService;
import com.company.project.core.AbstractService;
import com.company.project.util.JWTUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * Created by CodeGenerator on 2023/10/17.
 */
@Service
@Transactional
public class VtManagerServiceImpl extends AbstractService<VtManager> implements VtManagerService {
    @Resource
    private VtManagerMapper vtManagerMapper;


    @Override
    public List<OperatorVO> findOprator(Integer groupId) {
        VtManager vtManager = new VtManager();
        vtManager.setGroupid(groupId);
        byte state = 1;
        vtManager.setState(state);
        List<VtManager> vtManagers = vtManagerMapper.select(vtManager);
        List<OperatorVO> operatorVOList = new ArrayList<>();
        for (VtManager manager:
             vtManagers) {
            OperatorVO operatorVO = new OperatorVO();
            operatorVO.setUserId(manager.getUserid());
            operatorVO.setTrueName(manager.getTruename());
            operatorVOList.add(operatorVO);
        }
        return operatorVOList;
    }

    @Override
    public List<OperatorVO> findAssembleDirector() {
        VtManager vtManager = new VtManager();
        vtManager.setRoleid(7);
        byte state = 1;
        vtManager.setState(state);
        List<VtManager> vtManagers = vtManagerMapper.select(vtManager);
        List<OperatorVO> operatorVOList = new ArrayList<>();
        for (VtManager manager:
                vtManagers) {
            OperatorVO operatorVO = new OperatorVO();
            operatorVO.setUserId(manager.getUserid());
            operatorVO.setTrueName(manager.getTruename());
            operatorVOList.add(operatorVO);
        }
        return operatorVOList;
    }

    @Override
    public UserVO findUserByToken(String token) {
        DecodedJWT decodedJWT = JWTUtils.verify(token);
        Map<String, Claim> map = decodedJWT.getClaims();
        String userId = map.get("userId").asString();
        System.out.println(map.get("userId").asString());
        VtManager vtManager = vtManagerMapper.selectByPrimaryKey(Integer.parseInt(userId));
        UserVO userVO = new UserVO();
        if (Objects.isNull(vtManager)) return null;
        userVO.setUserId(vtManager.getUserid());
        userVO.setRoleId(vtManager.getRoleid());
        userVO.setGroupId(vtManager.getGroupid());
        userVO.setTrueName(vtManager.getTruename());
        userVO.setGender(vtManager.getGender());
        userVO.setNickName(vtManager.getNickname());
        userVO.setUserName(vtManager.getUsername());
        return userVO;
    }
}
